// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network;

import java.util.Collection;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import zombie.core.Rand;

public class IsoObjectID<T> implements Iterable<T> {
    public static final short incorrect = -1;
    private final ConcurrentHashMap<Short, T> IDToObjectMap = new ConcurrentHashMap();
    private final String objectType;
    private short nextID = (short)Rand.Next(32766);

    public IsoObjectID(Class<T> cls) {
        this.objectType = cls.getSimpleName();
    }

    public void put(short _short, T object) {
        if (_short != -1) {
            this.IDToObjectMap.put(_short, object);
        }
    }

    public void remove(short id) {
        this.IDToObjectMap.remove(id);
    }

    public void remove(T object) {
        this.IDToObjectMap.values().remove(object);
    }

    public T get(short _short) {
        return (T)this.IDToObjectMap.get(_short);
    }

    public int size() {
        return this.IDToObjectMap.size();
    }

    public void clear() {
        this.IDToObjectMap.clear();
    }

    public short allocateID() {
        this.nextID++;
        if (this.nextID == -1) {
            this.nextID++;
        }

        return this.nextID;
    }

    public Iterator<T> iterator() {
        return this.IDToObjectMap.values().iterator();
    }

    public void getObjects(Collection<T> out) {
        out.addAll(this.IDToObjectMap.values());
    }
}
